var app = getApp();
var common = require("../../utils/common.js");
Page({
    data:{
        host: app.d.hostUrl,
        page:0,
        currentTab: 0,
        //当前设备可用窗口的宽高
        winWidth: 0,
        winHeight: 0,
        orderList0:[],
        orderList1:[],
        orderList2:[],
        orderList3:[],

    },

    //初始化数据
    onLoad:function(options){
        var that = this;
        var currentTab = options.currentTab;
        that.initSystemInfo();
        that.setData({
            currentTab: parseInt(options.currentTab),
        });
        that.loadOrderList();
    },

    //请求当前订单信息
    loadOrderList:function(){
        var that = this;
        var currentTab = that.data.currentTab;
        var page = that.data.page;
        wx.request({
            url: app.d.hostUrl + '/api/orders/index',
            method: 'get',
            data: {
                user_id: app.d.userId,
                currentTab: parseInt(currentTab),
                page:page
            },
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },

            success:function(res){
                var status = res.data.status;
                var count = res.data.count;
                var data = res.data.info.ordersInfo;

                if(status == 1){
                    switch(currentTab){
                        case 0:
                        that.setData({
                            orderList0:data,
                        });

                        break;
                        case 1:
                        that.setData({
                            orderList1:data,
                        });
                        break;
                        case 2:
                        that.setData({
                            orderList2:data,
                        });
                        break;
                        case 3:
                        that.setData({
                            orderList3:data,
                        });
                        break;
                    }
                }else{
                    wx.showToast({
                        title: '暂无数据',
                        duration: 2000
                    });
                    return false;
                }

            }
        });
    },

    //获取当前设备信息
    initSystemInfo:function(){
        var that = this;
        wx.getSystemInfo({
          success: function(res) {
            that.setData({
                winWidth:res.windowWidth,
                winHeight:res.windowHeight
            });
          }
        })
    },

    //点击不同模块跳转函数
    swichNav:function(res){
        var that = this;
        if(res.target.dataset.current === that.data.currentTab){
            return false;
        }else{
            var currentTab = res.target.dataset.current;
            that.setData({
                currentTab:parseInt(currentTab),
            });

            switch(that.data.currentTab){
                case 0:
                    !that.data.orderList0.length && that.loadOrderList();
                    break;
                case 1:
                    !that.data.orderList1.length && that.loadOrderList();
                    break;
                case 2:
                    !that.data.orderList2.length && that.loadOrderList();
                    break;
                case 3:
                    !that.data.orderList3.length && that.loadOrderList();
                    break;
            }
        }
    },

    //根据左右滑动切换模块
    bindChange: function(e) {
        var that = this;
        that.setData( { currentTab: e.detail.current });
        that.loadOrderList();
    },
});